import {combination} from "../../utils/util";


class SkuCode {

    code
    spuId
    totalSegments = [] //保留一个skuCode所有可能的路径

    constructor(code) {
        this.code = code
        this._splitToSeqments()
    }

    _splitToSeqments() {
        // 2$1-44#3-9#4-14
        const spuAndSpec = this.code.split('$')
        this.spuId = spuAndSpec[0]
        const specCodeArray = spuAndSpec[1].split('#')
        const length = specCodeArray.length

        for (let i = 1; i <= length; i++) { // 比如：3选一多少组合，3选2，3选3呢
            const segments = combination(specCodeArray, i)
            const newSegments = segments.map(segs => {
                // 尽量少些显式的for循环
                // 把二维数组降为一维数组
                // join() 方法用于把数组中的所有元素放入一个字符串。#作为元素分隔符
                return segs.join('#')
            })
            this.totalSegments = this.totalSegments.concat(newSegments)
        }
    }

}

export {
    SkuCode
}